package com.xiaoke.api.controller;

import com.xiaoke.api.service.*;
import com.xiaoke.entity.*;
import com.xiaoke.shiro.SecurityService;
import com.xiaoke.util.common.AjaxMsg;
import com.xiaoke.util.common.ValidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private SecurityService securityService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private UserService userService;
    @Autowired
    private CollectService collectService;

    @GetMapping("/category")
    public AjaxMsg category(Integer id){
        Category currentCategory = categoryService.findById(id);
        Category parentCategory = categoryService.findById(currentCategory.getParentId());
        List<Category> brotherCategory = categoryService.findBrotherCategory(currentCategory.getParentId(),currentCategory.getId());

        Map res = new HashMap<>();
        res.put("currentCategory",currentCategory);
        res.put("parentCategory",parentCategory);
        res.put("brotherCategory",brotherCategory);
        return new AjaxMsg(res);
    }

    @GetMapping("/detail")
    public AjaxMsg detail(String id,HttpServletRequest request){
        Map<String,Object> res = goodsService.getDetailById(id);
        res.put("isCollect","00");
        String token = request.getHeader("token");
        User user = null;
        if(!ValidUtil.isEmpty(token)){
           user = userService.findByToken(token);
        }
        if(user != null){
            Collect collect = collectService.findByUserAndGoods(user.getId(), id);
            if(collect == null)
                res.put("isCollect","00");
            else
                res.put("isCollect","01");
        }
        return new AjaxMsg(res);
    }

    @GetMapping("/list")
    public AjaxMsg list(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestParam("categoryId") String categoryId){
        Map map =new HashMap();
        map.put("pageSize",page);
        map.put("pageNo",size);
        map.put("categoryId",categoryId);
        List<Map<String,Object>> data = goodsService.getByParams(map);

        Map res = new HashMap();
        res.put("goodsList",data);
        return new AjaxMsg(res);
    }

    @PostMapping("/count")
    public AjaxMsg count(HttpServletRequest request){
        String shopId = request.getHeader("shopId");
        return new AjaxMsg(goodsService.countByShop(shopId));
    }

    @GetMapping("/related")
    public AjaxMsg related(String id,HttpServletRequest request){
        String appId = request.getHeader("appId");
        Shop shop = shopService.findByAppId(appId);

        Goods goods = goodsService.selectByPrimaryKey(id);
        List<Map<String, Object>> goodsList = goodsService.findByCategoryAndShop(goods.getCategoryId(), shop.getId());

        Map res = new HashMap();
        res.put("goodsList",goodsList);
        return new AjaxMsg(res);
    }
}
